# coding=utf-8

import os
from moviepy.video.io.VideoFileClip import VideoFileClip

AUDIO_OR_VIDEO_SUFFIX = (
    '.mp3', '.wav', '.wma', '.flac', '.ape', '.aac', '.m4a', '.mp4', '.avi', '.mkv', '.wmv', '.mov', '.flv', '.rmvb',
    '.webm', '.3gp', '.rm', '.asf', '.ts', '.mpg', '.mpeg', '.vob')

AUDIO_SUFFIX = ('.mp3', '.wav', '.wma', '.flac', '.ape', '.aac', '.m4a')
VIDEO_SUFFIX = ('.mp4', '.avi', '.mkv', '.wmv', '.mov', '.flv', '.rmvb', '.webm', '.3gp', '.rm', '.asf', '.ts', '.mpg',
                '.mpeg', '.vob')
def check_exits_audio_or_video_file(file_path):
    """
    检查文件是否存在
    :param file_path: 文件路径
    :return: True or False
    """


    for root, dirs, files in os.walk(file_path):
        for file in files:
            if file.endswith(tuple(AUDIO_OR_VIDEO_SUFFIX)):
                return True

    return False

def video_to_audio(video_path, output_audio_path):
    """
    从视频中提取音频
    :return:
    """
    # audio_path = f"{os.path.splitext(video_path)[0]}.wav"  # "path/to/audio.wav"

    video = VideoFileClip(video_path)
    audio = video.audio
    audio.write_audiofile(output_audio_path)

    video.close()
    return output_audio_path

def write_to_text(file_path, content):
    """
    写入文本
    :param file_path: 文件路径
    :param content: 数据
    :return:
    """
    with open(file_path, 'w', encoding='utf-8') as f:

        f.write("\n".join(content) + '\n')
